package com.xoa.dev.efficiency.controller;

import com.xoa.dev.efficiency.service.FlowRunFuService;
import com.xoa.model.users.Users;
import com.xoa.model.workflow.FlowTypeModel;
import com.xoa.util.CookiesUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.common.wrapper.BaseWrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/FlowRunFu")
public class FlowRunFuController {
    @Autowired
    FlowRunFuService flowRunFuService;

    @RequestMapping("/administratorLogin") //管理员登录
    public String administratorLogin() {
        return "app/dev/efficiency/setting/administratorLogin";
    }

    @RequestMapping("/FormulaSetting") //重点工作公式设置
    public String FormulaSetting() {
        return "app/dev/efficiency/setting/FormulaSetting";
    }

    @RequestMapping("/KeyWorkflowSettings") //重点工作流水号设置
    public String KeyWorkflowSettings() {
        return "app/dev/efficiency/setting/KeyWorkflowSettings";
    }

    @RequestMapping("/ManagePasswordSettings") //效能监察管理密码设置
    public String ManagePasswordSettings() {
        return "app/dev/efficiency/setting/ManagePasswordSettings";
    }

    @RequestMapping("/MonitoringAuthoritysSettings")  //检查权限设置
    public String MonitoringAuthoritysSettings() {
        return "app/dev/efficiency/setting/MonitoringAuthoritysSettings";
    }

    @RequestMapping("/ProcessSelectionSettings") //工作流设置
    public String ProcessSelectionSettings() {
        return "app/dev/efficiency/setting/ProcessSelectionSettings";
    }

    @RequestMapping("/index")
    public String index() {
        return "app/dev/efficiency/index";
    }

    @RequestMapping("/KeyWorkConfirmation")
    public String KeyWorkConfirmation() {
        return "app/dev/efficiency/KeyWorkConfirmation";
    }

    @RequestMapping("/KeyWorkStatistics")
    public String KeyWorkStatistics() {
        return "app/dev/efficiency/KeyWorkStatistics";
    }

    @RequestMapping("/KeyWorkSupervision")
    public String KeyWorkSupervision() {
        return "app/dev/efficiency/KeyWorkSupervision";
    }

    @RequestMapping("/routineWorkStatistic")
    public String routineWorkStatistic() {
        return "app/dev/efficiency/routineWorkStatistic";
    }

    @RequestMapping("/routineWorkSupervision")
    public String routineWorkSupervision() {
        return "app/dev/efficiency/routineWorkSupervision";
    }


    //重点工作确认查询
    @RequestMapping("/queryFlowRunZhong")
    @ResponseBody
    public BaseWrappers queryFlowRunZhong(HttpServletRequest request,
                                          @RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                          @RequestParam(name = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                                          @RequestParam(name = "useFlag", required = false, defaultValue = "true") Boolean useFlag, HttpServletResponse response,
                                          @RequestParam(name = "runId", required = false) Integer runId,
                                          @RequestParam(name = "runName", required = false) String runName,
                                          @RequestParam(name = "state", required = false) String state,
                                          @RequestParam(name = "beginDate", required = false) String beginDate,
                                          @RequestParam(name = "endDate", required = false) String endDate,
                                          @RequestParam(name = "status", required = false) String status
    ) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionCookie);
        return flowRunFuService.queryFlowRunZhong(page, pageSize, useFlag, response, request, runId, runName, state, beginDate, endDate, status);
    }


    //重点工作查询
    @RequestMapping("/queryFlowRunZhongList")
    @ResponseBody
    public BaseWrappers queryFlowRunZhongList(HttpServletRequest request,
                                              @RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                              @RequestParam(name = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                                              @RequestParam(name = "useFlag", required = true, defaultValue = "false") Boolean useFlag, HttpServletResponse response,
                                              @RequestParam(name = "runId", required = false) Integer runId,
                                              @RequestParam(name = "runName", required = false) String runName,
                                              @RequestParam(name = "state", required = false) String state,
                                              @RequestParam(name = "beginDate", required = false) String beginDate,
                                              @RequestParam(name = "endDate", required = false) String endDate,
                                              @RequestParam(name = "status", required = false) String status,
                                              @RequestParam(name = "userId", required = false) String userId) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionCookie);
        return flowRunFuService.queryFlowRunZhongList(page, pageSize, useFlag, response, request, runId, runName, state, beginDate, endDate, status, userId);
    }


    //常规工作查询
    @RequestMapping("/queryFlowRunChang")
    @ResponseBody
    public BaseWrappers queryFlowRunChang(HttpServletRequest request,
                                          @RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                          @RequestParam(name = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                                          @RequestParam(name = "useFlag", required = false, defaultValue = "false") Boolean useFlag, HttpServletResponse response,
                                          @RequestParam(name = "runId", required = false) Integer runId,
                                          @RequestParam(name = "runName", required = false) String runName,
                                          @RequestParam(name = "state", required = false) String state,
                                          @RequestParam(name = "beginDate", required = false) String beginDate,
                                          @RequestParam(name = "endDate", required = false) String endDate,
                                          @RequestParam(name = "userId", required = false) String userId,
                                          @RequestParam(name = "status", required = false) String status) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionCookie);
        return flowRunFuService.queryFlowRunChang(page, pageSize, useFlag, response, request, runId, runName, state, beginDate, endDate, userId, status);
    }


    //重点工作统计
    @RequestMapping("/zhongTong")
    @ResponseBody
    public BaseWrappers zhongTong(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                  @RequestParam(name = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                                  @RequestParam(name = "useFlag", required = false, defaultValue = "false") Boolean useFlag,
                                  @RequestParam(name = "userIds", required = false) String userIds,
                                  @RequestParam(name = "deptIds", required = false) String deptIds,
                                  @RequestParam(name = "beginDate", required = false) String beginDate,
                                  @RequestParam(name = "endDate", required = false) String endDate,
                                  @RequestParam(name = "export", defaultValue = "0") Integer export,
                                  HttpServletRequest request, HttpServletResponse response) {
        return flowRunFuService.zhongTong(request, response, page, pageSize, useFlag, userIds, deptIds, beginDate, endDate, export);
    }

    //常规工作统计
    @RequestMapping("/changTong")
    @ResponseBody
    public BaseWrappers changTong(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                  @RequestParam(name = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                                  @RequestParam(name = "useFlag", required = false, defaultValue = "false") Boolean useFlag,
                                  @RequestParam(name = "userIds", required = false) String userIds,
                                  @RequestParam(name = "deptIds", required = false) String deptIds,
                                  @RequestParam(name = "beginDate", required = false) String beginDate,
                                  @RequestParam(name = "endDate", required = false) String endDate,
                                  @RequestParam(name = "export", defaultValue = "0") Integer export,
                                  HttpServletRequest request, HttpServletResponse response) {
        return flowRunFuService.changTong(request, response, page, pageSize, useFlag, userIds, deptIds, beginDate, endDate, export);
    }

    @RequestMapping("/KeySonWorkSupervision")
    public String KeySonWorkSupervision() {
        return "app/dev/efficiency/KeySonWorkSupervision";
    }

    //子流程重点工作查询
    @RequestMapping("/querySonFlowRunZhongList")
    @ResponseBody
    public BaseWrappers queryFlowRunZhongList(Integer parentRunId) {
        return flowRunFuService.queryFlowRunZhongList(parentRunId);
    }

    /**
     * 返回当前流程的父流程号或者子流程号
     *
     * @param flowId
     * @param runId
     * @return
     */
    @RequestMapping("/parentAndSon")
    @ResponseBody
    public ToJson parentAndSon(Integer flowId, Integer runId) {
        return flowRunFuService.parentAndSon(flowId, runId);
    }

    /**
     * 查询重点流程类型
     */
    @RequestMapping("/selectImportantFlowType")
    @ResponseBody
    public ToJson<FlowTypeModel> selectImportantFlowType() {
        try {
            return flowRunFuService.selectImportantFlowType();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ToJson<>(1, "err");
    }
}